# Java 类
from cn.test.entity.JavaMethod import JavaMethod
from cn.test.entity.JavaField import JavaField


class JavaObject(object):
    def __init__(self, pkgName, modify, isAbstract, isInter, className, extendClass, implements=None,
                 methods=None, fields=None):
        self.implements = implements
        self.extendClass = extendClass
        self.pkgName = pkgName
        self.modify = modify
        self.isAbstract = isAbstract
        self.isInter = isInter
        self.className = className
        self.methods = methods
        self.fields = fields

    def __str__(self):
        objectStr = "package " + self.pkgName + " \n" + self.modify + " "
        if self.isAbstract:
            objectStr += " abstract class "
        if self.isInter:
            objectStr += " interface "
        else:
            objectStr += " class "
        objectStr += self.className
        objectStr += " extend " + self.extendClass
        if self.implements is not None and len(self.implements) > 0:
            objectStr += " implements "
            for ret in self.implements:
                objectStr += ret
        objectStr += " { \n"
        if self.fields is not None and len(self.fields) > 0:
            for ret in self.fields:
                objectStr += " " + str(ret) + " \n"
        if self.methods is not None and len(self.methods) > 0:
            for ret in self.methods:
                objectStr += " " + str(ret) + "\n"
        return objectStr + "\n }"

    __repr__ = __str__


if __name__ == '__main__':
    field1 = JavaField('private', False, True, 'String', 'textStr', '\"test\"')
    field2 = JavaField('private', False, True, 'int', 'textInt', '1')
    field3 = JavaField('private', False, True, 'long', 'textLong', '100L')
    field4 = JavaField('private', False, True, 'double', 'textDouble', '1.2')
    method1 = JavaMethod('public', False, False, True, 'void', 'testMethod1', 'System.out.println(\"测试方法\")', [],
                         {'String': 'tester'})
    method2 = JavaMethod('public', False, False, True, 'void', 'testMethod2', 'System.out.println(\"测试方法\")', [],
                         {'int': 'val'})
    method3 = JavaMethod('public', False, False, True, 'void', 'testMethod3', 'System.out.println(\"测试方法\")', [],
                         {'Object': 'obj'})
    javaObj = JavaObject('public', False, False, 'TestClass', methods=[method1, method2, method3],
                         fields=[field1, field2, field3, field4])
    print(javaObj)
